package com.jiuye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiuye.common.CustomException;
import com.jiuye.entity.Dish;
import com.jiuye.entity.DishDto;
import com.jiuye.entity.DishFlavor;
import com.jiuye.mapper.IDishMapper;
import com.jiuye.service.IDishFlavorService;
import com.jiuye.service.IDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 菜品业务层实现类
 *
 * @author likeWen
 * @since 2022/11/4 15:55
 */
@Service
public class DishServiceImpl extends ServiceImpl<IDishMapper, Dish> implements IDishService {
    @Autowired
    private IDishFlavorService dishFlavorService;

    /**
     * 添加菜品信息及口味
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //1 保存菜品基本信息
        //单一原则
        Dish dish = new Dish();
        //第一个参数是源数据
        //第二个参数是目标
        //BeanUtils.copyProperties 可以做到类复制
        BeanUtils.copyProperties(dishDto,dish);
        save(dish);
        //2.保存菜品口味信息
        //需要获得这个菜品的id
        Long dishId = dish.getId();
        //保存菜品口味信息
        //保存之前，要给这些菜品口味的dishId赋值
        //如果不复制，那么这些口味实际上就不属于任何菜品了
        //也就不存在关联了
        //size大于0，就代表前端传入了口味信息
        if (dishDto.getFlavors().size()>0){
            for (DishFlavor flavor : dishDto.getFlavors()) {
                //菜品id
                flavor.setDishId(dishId);
            }
            //调用保存菜品口味信息
            this.dishFlavorService.saveBatch(dishDto.getFlavors());
        }
    }

    /**
     * 根据id获取菜品及菜品口味
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品的基本信息
        Dish dish = this.getById(id);
        //校验
        if (dish == null){
            throw new CustomException("传入参数有问题");
        }
        //构建dishDto,并赋值
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        // 查询当前菜品对应的口味信息,赋值给dishDto对象
        // 要去菜品口味表中查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        //返回的是当前菜品的口味信息
        List<DishFlavor> list = this.dishFlavorService.list(queryWrapper);
        //赋值给dishDto对象
        dishDto.setFlavors(list);
        return dishDto;
    }

    /**
     * 更新菜品信息
     * @param dto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dto) {
        //1.先更新Dish表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        this.updateById(dish);
        //2.删除原来的菜品口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        this.dishFlavorService.remove(queryWrapper);
        //3.把dishId重新赋值给flavors口味
        for (DishFlavor flavor : dto.getFlavors()) {
            flavor.setDishId(dish.getId());
        }
        dto.getFlavors().forEach(item ->item.setDishId(dish.getId()));
        // 4.添加新的口味
        this.dishFlavorService.saveBatch(dto.getFlavors());
    }
}
